package cn.io0288.blog2.db;

import cn.io0288.blog2.model.Log;
import cn.io0288.blog2.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Logs {
    public static ArrayList<Log> getLogs() {
        Connection connection;
        PreparedStatement preparedStatement;
        ResultSet resultSet;

        ArrayList<Log> logs = new ArrayList<>();

        connection = JdbcUtils.getConnection();
        String sql = "SELECT * FROM logs ORDER BY `date` DESC LIMIT 5";

        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Log l = new Log(resultSet);
                logs.add(l);
            }
            System.out.println("articles count: " + logs.size());
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return logs;
    }
}





